package com.yuntian.controller;

import com.yuntian.dto.InventoryConfigDto;
import com.yuntian.exception.BusinessException;
import com.yuntian.service.InventoryConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
public class InventoryConfigController {
    @Autowired
    private InventoryConfigService inventoryConfigService;

    // 1. 初期显示：GET请求，直接返回页面
    @RequestMapping(value = "/PR10501", method = RequestMethod.GET)
    public String initPage() {
        // 无需传参，直接返回页面，实现“画面初期表示”
        return "PR10501";
    }

    @RequestMapping( "/PR10501")
    public String getProductData(@RequestParam(name="M00201",required = false) String M00201, Model model){




        if (M00201==null||M00201.trim().isEmpty()){
            model.addAttribute("errorMsg","商品コードを入力してください。");
            model.addAttribute("M00201", M00201);
            return "PR10501";
        }



        try {
            InventoryConfigDto inventoryConfigDto = inventoryConfigService.getProductData(M00201.trim());
            model.addAttribute("inventoryConfigDto", inventoryConfigDto);
            model.addAttribute("M00201", M00201.trim());
        } catch (BusinessException e) {
            // 捕获业务异常（如商品不存在），显示业务错误信息
            model.addAttribute("errorMsg", e.getErrorMsg());
            model.addAttribute("M00201", M00201.trim());
        } catch (Exception e) {
            // 捕获系统异常（如SQL错误），显示系统错误信息
            model.addAttribute("errorMsg", "M00001E：ＳＱＬ実行例外が発生しました。");
            model.addAttribute("M00201", M00201.trim());
            e.printStackTrace();
        }
        return "PR10501";
    }




}
